<?php
/**
 * Created by JetBrains PhpStorm.
 * User: T420
 * Date: 11/13/12
 * Time: 5:29 PM
 * To change this template use File | Settings | File Templates.
 */
class Models_Orders extends Libs_Model
{
    public $totalpage;
    public function __construct()
    {
        parent::__construct();
    }
    public function getorder($pagenum,$productperpage,$stt)//lay danh sach dat hang theo trang thai
    {

        $this->db->connect();
        if($stt!=-1)//lay theo status
        {
            $sqlstr="select a.oid,namecustomer,postdate,datereceipt,sum(price*quantity) as tong,ostatus from lb_order a, lb_orderdetail b  where a.oid=b.oid and a.ostatus='$stt'
        group by a.oid ";

        }
        else //lay toan bo danh sach dat hang
            $sqlstr="select a.oid,namecustomer,postdate,datereceipt,sum(price*quantity) as tong,ostatus from lb_order a, lb_orderdetail b where a.oid=b.oid
        group by a.oid ";
        $this->totalpage=count($this->db->executeQuery($sqlstr));
        if(!$pagenum || $pagenum<=0 || $pagenum>$this->totalpage)
            $pagenum=1;
        if($pagenum==1)
            $from=0;
        else
            $from=($pagenum-1)*$productperpage;
        $sqlstr.="order by a.oid DESC limit $from,$productperpage";
        $rs=$this->db->executeQuery($sqlstr);
         $this->db->disconnect();
        return $rs;
    }
    public function getvieworderdetail($id)
    {
        $this->db->connect();
        $rs=$this->db->executeQuery("select * from lb_orderdetail where oid='$id' ");
        $this->db->disconnect();
        return $rs;
    }
    public function getvieworder($id)
    {
        $this->db->connect();
        $rs=$this->db->executeQuery("select * from lb_order a, lb_payment b where a.payid=b.payid and oid='$id' ");
        $this->db->disconnect();
        return $rs;
    }
    public function getproduct($id)
    {
        $this->db->connect();
        $rs=$this->db->executeQuery("select * from lb_product where pid='$id' ");
        $this->db->disconnect();
        return $rs;
    }
    public function delorder($id)
    {
        $this->db->connect();
        $rs=$this->db->query("delete from lb_order where oid in ('$id')");
        $this->db->disconnect();

    }
    function updatestt($id,$stt,$oid)
    {
        $this->db->connect();
        $rs=$this->db->query("update lb_order set emid='$id',ostatus='$stt' where oid='$oid'");
        $this->db->disconnect();
    }
    function updateordertail($odid,$qty,$price)
    {
        $this->db->connect();
        $rs=$this->db->query("update lb_orderdetail set quantity='$qty',price='$price' where odid='$odid'");
        $this->db->disconnect();
    }
}
